# -*- coding: utf8 -*-

class Statistics:
    
    _stats = None
    _lock = None
    _lock_upd = None
    _updated = False
    
    def __init__( self, stats = {} ):
        self.reset()
        self._stats.update(stats)

    def reset( self ):
        self._stats = {
            "errors_count" : 0,
            "files_count" : 0,
            "files_processed" : 0,
            "files_filtered" : 0,
            "files_size" : 0.0,
            "archives_count" : 0,
            "archives_processed" : 0,
            "archives_size" : 0,
            "archives_processed_size" : 0,
            "images_count" : 0,
            "images_processed" : 0,
            "images_size" : 0.0,
            "real_readed_size" : 0.0,
            "readed_size" : 0.0,
            "directories_count" : 0,
            "directories_searched" : 0,
            "hashed_count" : 0,
            "unique_count" : 0,
            "nonunique_count" : 0,
            "nonunique_size" : 0,
            "nonunique_count_now" : 0,
            "nonunique_size_now" : 0,
            "hashing_file":{},
            "images_archive":{
                "name" : "",
                "count" : 0,
                "processed" : 0,
                "start_time" : 0
            }
        }
        return self
    
    def set( self, name, value ):
        self._stats[name] = value
        self.updated()
        return self
    
    def inc( self, name, plus ):
        self._stats[name] += plus
        self.updated()
        return self

    def dec( self, name, minus ):
        self._stats[name] -= minus
        self.updated()
        return self

    def set_hashing( self, device, name, size, readed, start_time):
        self._stats["hashing_file"][ str(device) ] = {
            "name" : name,
            "size" : size,
            "readed" : readed,
            "start_time" : start_time
        }
        self.updated()
        return self

    def inc_hashing( self, device, readed):
        self._stats["hashing_file"][ str(device) ]["readed"] += readed
        self.updated()
        return self

    def set_imaging( self, name, count, processed, start_time):
        self._stats["images_archive"] = {
            "name" : name,
            "count" : count,
            "processed" : processed,
            "start_time" : start_time
        }
        self.updated()
        return self

    def inc_imaging( self, processed):
        self._stats["images_archive"]["processed"] += processed
        self.updated()
        return self


    def get_update( self ):
        s = {}
        s.update( self._stats )
        self._updated = False
        return s
    
    def is_updated( self ):
        u = self._updated
        return u
    
    def updated( self ):
        self._updated = True
        return self
    
    pass
